package com.vids.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vids.model.entity.Face;
import com.vids.model.param.FaceBatchModel;
import com.vids.model.param.FaceBatchParam;
import com.vids.model.param.SubImageBatchParam;
import com.vids.model.param.FaceModel;
import com.vids.model.vo.SubImageInfoVo;
import com.vids.mapper.FaceMapper;
import com.vids.service.IFaceService;
import com.vids.service.IImageBusinessService;
import com.vids.utils.BeanUtilss;
import com.vids.utils.QueryUtils;
import com.vids.mapper.FaceMapper;
import com.vids.model.entity.Face;
import com.vids.service.IFaceService;
import com.vids.service.IImageBusinessService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 人像 服务实现类
 * </p>
 *
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class FaceServiceImpl extends ServiceImpl<FaceMapper, Face> implements IFaceService {

    public final static String type = "11";

    private final FaceMapper faceMapper;

    private final IImageBusinessService imageBusinessServiceImpl;

    public Face findByFaceID(String FaceID) {
	return baseMapper.findByFaceID(FaceID);
    }

    @Override
    public void deleteByFaceID(String FaceID) {
	baseMapper.deleteByFaceID(FaceID);
    }

    @Override
    public FaceBatchParam getFaceList(Map<String, Object> map) {
	// 无条件查询默认返回所有
	if (map == null || map.size() == 0) {
	    List<Face> resultList = faceMapper.findAll();
	    return buildResultModel(resultList);
	}
	Map<String, Object> paramMap = QueryUtils.buildParam(map);
	List<Face> resultList = faceMapper.getFaceList(paramMap);
	return buildResultModel(resultList);
    }

    private FaceBatchParam buildResultModel(List<Face> resultList) {
	if (resultList != null && resultList.size() > 0) {
	    FaceBatchParam param = new FaceBatchParam();
	    FaceBatchModel batchModel = new FaceBatchModel();
	    List<FaceModel> modelList = new ArrayList<>();
	    for (Face face : resultList) {
		FaceModel model = BeanUtilss.transferObjectCommon(face, FaceModel.class);
		List<SubImageInfoVo> volist = imageBusinessServiceImpl.findSubImageInfoVo(face.getFaceID(), type);
		SubImageBatchParam imageBatchParam = new SubImageBatchParam();
		imageBatchParam.setSubImageInfoObject(volist);
		model.setSubImageList(imageBatchParam);
		modelList.add(model);
	    }
	    batchModel.setFaceObject(modelList);
	    param.setFaceListObject(batchModel);
	    return param;
	}
	return null;
    }
}
